<?php
class Soul_Request_Context_Helper{
	
	public static $functions = array();
    
    private function __construct(){}
	
    public function __call($methodName, $arguments) {
    	if(self::functionIsRegistry($methodName)){
	    	$functionName = self::$functions[$methodName];
            if(is_array($functionName)){
                $obj = Soul_Loader::loadSingleton($functionName[0]);
                return call_user_func_array(array($obj, $functionName[1]), $arguments);
            }else{
                return call_user_func_array($functionName, $arguments);
            }
    	}
    	throw new Exception('Not found request context method ['.$methodName.']');
    }
    
    public static function registerClass($className){
        $refClass = new ReflectionClass($className);
        $methods = $refClass->getMethods(ReflectionMethod::IS_PUBLIC);
        foreach($methods as $method){
            if(Soul_Util_String::endsWith($method->getName(), 'Helper')){
                self::registerFunction(substr($method->getName(), 0, -6), $method->getName(), $refClass->getName());
            }
        }
    }
	
	public static function registerFunction($methodName, $functionName, $className = NULL){
        if(!is_null($className)){
            self::$functions[$methodName] = array($className, $functionName);
        }else{
            self::$functions[$methodName] = $functionName;
        }
	}
    
    public static function functionIsRegistry($methodName){
    	return isset(self::$functions[$methodName]);
    }
    
    private static $_instance = NULL;
    public static function getInstance(){
    	if(is_null(self::$_instance)){
    		self::$_instance = new self();
    	}
    	return self::$_instance;
    }
}